package com.cn.why.film.filmindex.service.serviceImpl;

import com.cn.why.film.filmindex.aop.MyRedisCache;
import com.cn.why.film.filmindex.entity.Order;
import com.cn.why.film.filmindex.entity.PetProduce;
import com.cn.why.film.filmindex.entity.ShopCar;
import com.cn.why.film.filmindex.repository.OrderRepository;
import com.cn.why.film.filmindex.repository.PetMartReposity;
import com.cn.why.film.filmindex.repository.ShopCarRepository;
import com.cn.why.film.filmindex.service.PetMartService;
import com.cn.why.film.filmindex.util.ProducerMsg;
import io.swagger.models.auth.In;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 宠物商品实现层
 */
@Service
@Transactional
public class PetMartServiceImpl implements PetMartService {
    @Autowired
    private PetMartReposity proReposity;
    @Autowired
    private ShopCarRepository shopCarRepository;
    @Autowired
    private ProducerMsg sendMsg;
    @Autowired
    private OrderRepository orderRepository;

    /**
     * 查询宠物商品的信息
     * @return
     */
    @Override
    public List<PetProduce> selectProByMsg(String msg1, String msg2) {
        List<PetProduce> list = new ArrayList<>();
        //判断是否有搜索条件，如果为All时搜索所有
        if(!"Find".equals(msg2) && "petPro".equals(msg1) && !"".equals(msg2)){ //搜索商品
            list = proReposity.selectProByMsg(msg2);
        }else {
            //查询所有
            list = findAllProduce();
        }
        return list;
    }

    /**
     * 查询所有商品信息
     * @return
     */
    @Override
    @MyRedisCache(key = "AllProList",expired = 300)
    public List<PetProduce> findAllProduce() {
        return proReposity.findAll();
    }

    /**
     * 获得总页数，默认每页展示6条
     * @return
     */
    @Override
    public Long findCount(){
        return proReposity.count()/6;
    }

    /**
     * 根据id查询商品
     */
    @Override
    public PetProduce findOneById(Integer id){
        return proReposity.findOne(id);
    }

    /**
     * 根据号码查询
     * @param phoneNumber
     * @return
     */
    @Override
    public List<ShopCar> findOneShopCar(Integer phoneNumber) {
        return shopCarRepository.findOneShopCar(phoneNumber);
    }

    /**
     * 购买操作
     * @param id
     * @return
     */
    @RabbitListener(queues = "QUEUE_B")
    @RabbitHandler
    public boolean buyFromProduce_pro(String phone,String adress,Integer id, Integer number,Map<String, Object> content) {
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("number",number);
        //通过mq查询订单系统
        sendMsg.sendMsg(map,"EXCHANGE_A","ROUTINGKEY_A");
        if(content.get("msg").equals("success")){
            PetProduce produce = (PetProduce) content.get("produce");
            Order order = new Order(UUID.randomUUID().toString());
            order.setProId(id);
            order.setAdress(adress);
            order.setPhone(phone);
            order.setNumber(number);
            order.setTotalPrice(number * produce.getPrice());
            orderRepository.save(order);
            return true;
        }
        return false;
    }
}
